package com.ljf.admin.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ljf.admin.POJO.SysUser;
import com.ljf.admin.POJO.VO.TableDataObject;
import com.ljf.admin.service.SysUserService;
import com.ljf.framework.log.LjfLogManager;
import com.ljf.framework.log.LjfLogService;
import com.ljf.framework.password.PasswordEncoderManager;
import com.ljf.framework.result.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Auther: lijinfeng
 * @Date: 2022/4/26
 * @Description 描述:
 */
@RestController
@RequestMapping("/system/user")
public class UserController {

//    LjfLogService logger = LjfLogManager.getLogger("LoginController");

    @Autowired
    private SysUserService sysUserService;
    
//    @Value("${file.path}")
//    private String filePath;


    @GetMapping("/all")
    public JsonResult getUserListTest(){
        sysUserService.list();
//        logger.info("访问了所有用户");
        return JsonResult.success(sysUserService.list());
    }

    @GetMapping("/list")
    public JsonResult getUserList(String query, Long currentPage, Long pageSize){

        Page<SysUser> page = new Page<>(currentPage,pageSize);

        QueryWrapper<SysUser> queryWrapper = new QueryWrapper<>();
        if(StringUtils.hasText(query)) {
            queryWrapper.like("username", query);
        }

        Page<SysUser> userPage = sysUserService.page(page,queryWrapper);
        long total = userPage.getTotal();
        List<SysUser> records = userPage.getRecords();
        return JsonResult.success(new TableDataObject(records,total));
    }

    @PostMapping
    public JsonResult addUser(@RequestBody SysUser user){


        String password = user.getPassword();
        user.setPassword(PasswordEncoderManager.encode(password));
        boolean save = sysUserService.save(user);
        if(save)
            return JsonResult.success("添加用户成功");
        return JsonResult.error("添加用户失败");

    }

    @DeleteMapping("/{userId}")
    public JsonResult deleteUser( @PathVariable String userId){
        System.out.println("删除用户信息"+userId);

        if("1".equals(userId))
            return JsonResult.error("admin用户不可以删除");

        boolean save = sysUserService.removeById(userId);
        if(save)
            return JsonResult.success("删除用户成功");
        return JsonResult.error("删除用户失败");

    }

    @PutMapping
    public JsonResult updateUser( @RequestBody SysUser user){
        if(ObjectUtils.isNotEmpty(user.getId()) && 1L == (user.getId()))
            return JsonResult.error("admin用户不可以修改");

        String password = user.getPassword();
        if(!password.startsWith("$")){
            user.setPassword(PasswordEncoderManager.encode(password));
        }

        UpdateWrapper<SysUser> sysUserUpdateWrapper = new UpdateWrapper<>();
        boolean save = sysUserService.updateById(user);
        if(save)
            return JsonResult.success("修改用户成功");
        return JsonResult.error("修改用户失败");

    }

//    @Transactional(value ="systemTransactionManager")
//    @OperationLog(title = "用户管理-导入")
//    @PostMapping("/upload")
//    public JsonResult upLoadBuyProduct(@RequestParam("file") MultipartFile file)  {
//        String localeFile = FileUtils.saveFileToLocale(file,filePath);
//        ExcelReader excelReaderUser = EasyExcelFactory.read(localeFile, ExcelUser.class, new DataListener<>(sysUserService)).build();
//        ReadSheet readSheetUser = EasyExcelFactory.readSheet(0).build();
//        excelReaderUser.read(readSheetUser);
//        // 这里千万别忘记关闭，读的时候会创建临时文件，到时磁盘会崩的
//        excelReaderUser.finish();
//        return JsonResult.success("上传成功");
//    }

}
